var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp()
Page({
  data: {
    keywrod: '',
    searchStatus: false,
    goodsList: [],
    helpKeyword: [],
    historyKeyword: [],
    categoryFilter: false,
    currentSortType: 'default',
    currentSortOrder: '',
    filterCategory: [],
    defaultKeyword: {},
    hotKeyword: [],
    page: 1,
    size: 10,
    currentSortType: 'id',
    currentSortOrder: 'desc',
    categoryId: 0,
    categoryName:'分类',
  },
  //取消搜索事件处理函数
  closeSearch: function () {
    wx.navigateBack()
  },
  //删除搜锁框中的关键字处理函数
  clearKeyword: function () {
    this.setData({
      keyword: '',
      searchStatus: false
    });
  },
  onLoad: function () {
    this.getSearchKeyword();
  },
  //查询关键字
  getSearchKeyword() {
    let that = this;
    util.request(api.SearchIndex).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          historyKeyword: res.data.historyKeywordList,
          defaultKeyword: res.data.defaultKeyword,
          hotKeyword: res.data.hotKeywordList
        });
      }
    });
  },
  //键盘输入时触发
  inputChange: function (e) {
    this.setData({
      keyword: e.detail.value,
      searchStatus: false
    });
    this.getHelpKeyword();
  },
  //搜索帮助
  getHelpKeyword: function () {
    let that = this;
    util.request(api.SearchHelper, { keyword: that.data.keyword }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          helpKeyword: res.data
        });
      }
    });
  },
  //搜索框获得焦点事件函数
  inputFocus: function () {
    this.setData({
      searchStatus: false,
      goodsList: []
    });

    if (this.data.keyword) {
      this.getHelpKeyword();
    }
  },
  //清除历史记录
  clearHistory: function () {
    this.setData({
      historyKeyword: []
    })

    util.request(api.SearchClearHistory, {}, 'POST')
      .then(function (res) {
        console.log('清除成功');
      });
  },
  //请求商品列表
  getGoodsList: function () {
    let that = this;
    util.request(api.GoodsList, {
      keyword: that.data.keyword, 
      page: that.data.page, 
      size: that.data.size, 
      sort: that.data.currentSortType, 
      order: that.data.currentSortOrder, 
      categoryId: that.data.categoryId 
      }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          searchStatus: true,
          categoryFilter: false,
          goodsList: that.data.goodsList.concat(res.data.goodsList),
          filterCategory: res.data.filterCategory,
        });
        res.data.filterCategory.filter(function (v) {
          if (v.checked == true) {
            that.setData({
              categoryName: v.name
            });
          }
        });
      }
      //重新获取关键词
      that.getSearchKeyword();
    });
  },
  //关键字点击事件
  onKeywordTap: function (event) {
    this.getSearchResult(event.target.dataset.keyword);
  },
  //得到搜索结果
  getSearchResult(keyword) {
    this.setData({
      keyword: keyword,
      page: 1,
      categoryId: 0,
      goodsList: []
    });
    this.getGoodsList();
  },
  //筛选条件
  openSortFilter: function (event) {
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'categoryFilter':
        this.setData({
          categoryFilter: !this.data.categoryFilter,
          currentSortType: 'category',
          currentSortOrder: 'asc'
        });
        break;
      case 'priceSort':
        let tmpSortOrder = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmpSortOrder = 'desc';
        }
        this.setData({
          currentSortType: 'price',
          currentSortOrder: tmpSortOrder,
          categoryFilter: false,
          page: 1,
          goodsList: [],
        });
        this.getGoodsList();
        break;
      default:
        //综合排序
        this.setData({
          currentSortType: 'default',
          currentSortOrder: 'desc',
          categoryFilter: false,
          page: 1,
          goodsList: [],
        });
        this.getGoodsList();
    }
  },
  //选择分类
  selectCategory: function (event) {
    let currentId = event.target.dataset.categoryId;
    this.setData({
      categoryFilter: false,
      categoryId: currentId,
      page: 1,
      goodsList: []
    });
    this.getGoodsList();
  },
  //点击完成按钮时触发
  onKeywordConfirm(event) {
    this.getSearchResult(event.detail.value);
  },
  //增加下拉刷新数据的功能
  onPullDownRefresh() {
    var that = this;
    if (!that.data.keyword || !that.data.searchStatus){
      that.getSearchKeyword();
      return;
    }
    that.setData({
      page: 1,
      goodsList: [],
    });
    that.getGoodsList();
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    var that = this;
    if (!that.data.keyword || !that.data.searchStatus) {
      return;
    }
    that.setData({
      page: that.data.page + 1,
    });
    that.getGoodsList();
  },
})